const log4js = require('koa-log4');
const logger = log4js.getLogger('routes::staff');
const router = require('koa-router')();
exports = module.exports = router;
const mongodb = require('mongodb');
const ObjectId = mongodb.ObjectId;
const template = require('art-template');

const _ = require('lodash');
const util = require(global.prefixPath + '/commons/util');
const user_service = require(global.prefixPath + '/services/user');
const info_service = require(global.prefixPath + '/services/info');
template.defaults.imports.dateFormat = function(UTCDateString) {
    if (!UTCDateString) {
        return '-';
    }

    function formatFunc(str) { //格式化显示
        return str > 9 ? str : '0' + str
    }
    var date2 = new Date(UTCDateString); //这步是关键
    var year = date2.getFullYear();
    var mon = formatFunc(date2.getMonth() + 1);
    var day = formatFunc(date2.getDate());
    var hour = date2.getHours();
    // var noon = hour >= 12 ? 'PM' : 'AM';
    // hour = hour>=12?hour-12:hour;
    hour = formatFunc(hour);
    var min = formatFunc(date2.getMinutes());
    var sec = formatFunc(date2.getSeconds());
    var dateStr = year + '-' + mon + '-' + day + ' ' + ' ' + hour + ':' + min + ':' + sec;
    return dateStr;
};
router.get('/:city/percenter', async (ctx, next) => {
    console.log('输出信息哈哈哈哈哈');
	if (!ctx.hasLogin) {
        let city = ctx.params.city;
        return ctx.redirect('/:city/signin');
    } else {
		try {
            console.log('输出信息哈哈哈哈哈2');
			let now_page = 1
			let info_user = ctx.state.userinfo.username
			// console.log(info_collection)
			let q = {
				query: {
					user: info_user
				},
				sort: {
					createdAt: 1
				},
				page: {
					index: now_page,
					size: 10
				}
			}
            // 缺少个批量删除的信息
			console.log('info_user:' + info_user);
			let result = await info_service.find(q);
            if (ctx.state.userinfo.pub)  {
                let info_collection = ctx.state.userinfo.pub
    			let collect = await info_service.findByIds(info_collection);
                ctx.state.info_collect = collect;
            } else {
                let info_collection = [];
    			let collect = await info_service.findByIds(info_collection);
                ctx.state.info_collect = collect;
            }

			// console.log(result);
			ctx.state.info_person = result;

			// console.log(result[0]._id);
			// console.log(ctx.state.info_person);
		} catch (err) {
			throw err;
		}
	}
	return await ctx.render('percenter/index');
});

router.get('/:city/percenter/:num', async (ctx, next) => {
	if (!ctx.hasLogin) {
        return ctx.redirect('/:city/signin');
    } else {
		try {
            console.log('输出信息哈哈哈哈哈3');
            console.log(ctx.state.userinfo.pub);
			// console.log(ctx.params.id);
			let now_page = ctx.params.num;
			let info_user = ctx.state.userinfo.username
			// let info_collection = (ctx.state.userinfo.pub).split(',');
			let q = {
				query: {
					user: info_user
				},
				sort: {
					createdAt: 1
				},
				page: {
					index: now_page,
					size: 10
				}
			}

			// console.log('info_user:' + info_user);
			let result = await info_service.find(q);
            if (ctx.state.userinfo.pub)  {
                let info_collection = ctx.state.userinfo.pub
    			let collect = await info_service.findByIds(info_collection);
                ctx.state.info_collect = collect;
            } else {
                let info_collection = null;
    			let collect = await info_service.findByIds(info_collection);
                ctx.state.info_collect = collect;
            }
			// console.log(result);
			ctx.state.info_person = result;
			// console.log(result[0]._id);
			// console.log(ctx.state.info_person);
		} catch (err) {
			throw err;
		}
	}
	return await ctx.render('percenter/index');
});
